package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Oilfield;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.Office;
import cn.eonshine.cuba.lsms.modular.basic.service.IOfficeService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOilfieldService;

@Service
public class OilfieldService extends CommonService<Oilfield> implements IOilfieldService{

	@Autowired
	private IOfficeService officeService;
	
	private final String listUrl = "/oilfield/list";
	private final String updateUrl = "/oilfield/update";
	/**
	 * 请求油品数据
	 */
	@Override
	public void requestOil() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.EPEP_NUM);
	}
	
	@Override
	protected Object dataHandle(Oilfield oilfield) throws Exception {
		QueryWrapper<Office> entity = new QueryWrapper<Office>();
		Office info = null;
		if(StringUtils.isBlank(oilfield.getId())) {
			entity.eq("CODE",oilfield.getCode());
			entity.eq("TYPE", InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_OILFIELD);
			info = officeService.getOne(entity);
		}else {
			info = officeService.getById(oilfield.getId());
		}
		boolean isInsert = false;
		if (info == null) {
			info = new Office();
			isInsert = true;
		}

		info.setType(InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_OILFIELD);
		info.setCode(oilfield.getCode());
		info.setName(StringUtils.trim(oilfield.getName()));
		info.setFullName(StringUtils.trim(oilfield.getFullName()));
		info.setUseable(oilfield.getUseable());
		info.setIsFromInf(InfStatus.IS_FROM_INTERFACE);
		if(isInsert) {
			info.setProvince(oilfield.getProvince());
			info.setAddress(oilfield.getAddress());
			info.setZipCode(oilfield.getZipCode());
			info.setMaster(oilfield.getMaster());
			info.setPhone(oilfield.getPhone());
			info.setEmail(oilfield.getEmail());
			info.setParentId(InfStatus.LSMS_OFFICE_PARENT.ROOT);
			info.setParentIds(InfStatus.LSMS_OFFICE_PARENT.ROOT+InfStatus.SEPARATOR_CHAR);
			info.setCreateBy(InfStatus.CREATE_BY);
			if(oilfield.getCreateDate() != null) {
				info.setCreateDate(oilfield.getCreateDate());
			}else {
				info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			}
		}		
		
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		
		if (isInsert) {
			officeService.save(info);
		} else {
			officeService.updateById(info);
		}
		return info;
	}
	@Override
	protected List<Oilfield> feadBackDataHandle(Object obj, Oilfield oilfield, List<Oilfield> feadBackDataLists) throws Exception {
		Office info = (Office) obj;
		Oilfield tmpBack = new Oilfield();
		tmpBack.setCode(oilfield.getCode());
		tmpBack.setName(info.getName());
		tmpBack.setDataInputDatetime(oilfield.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setId(info.getId());
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
